-- region ModalDlgManager.lua
-- Author : nimol&soso
-- Date   : 2015/6/4
-- Description ：模态框管理 
require "common/ModalDlg"
ModalDlgManager = class("ModalDlgManager",function()
        local obj = { 
        }
        return obj
    end)
ModalDlgManager.__index = ModalDlgManager 
-- 单例设计
ModalDlgManager.__instance = nil 


-- 创建模态框管理器
function ModalDlgManager:create() 
    local obj = ModalDlgManager:new() 
    obj:init()
    ModalDlgManager.__instance = obj 
    return obj
end 


-- 获取单例
function ModalDlgManager:ins()
    if nil == ModalDlgManager.__instance then
        ModalDlgManager:create()
    end
    return ModalDlgManager.__instance
end 


-- 初始化函数
function ModalDlgManager:init()
end


function ModalDlgManager:getDlg(dlgid,closeFunc,view) 
	nOpacity = nOpacity or 0 
	closeFunc = closeFunc or function()end;
	local layer = cc.Layer:create()
	local touchrect = nil; 
	if dlgid.rect then 
		local winsize=cc.Director:getInstance():getWinSize();
		local h = 640;
		local w1= 960;
		local w2= 1136;  
		local curtype=1; 
		if (math.abs(winsize.width-w1) > math.abs(winsize.width-w2)) then 
			curtype = 2;
		end
		local offx=0;
		local offy=(winsize.height-h)/2;
		if curtype == 2 then 
		   offx=(w2-w1)/2
		end
		print('curtype=',curtype,offx,offy)
		touchrect = cc.rect(dlgid.rect[1]+offx,dlgid.rect[2]+offy,dlgid.rect[3]-dlgid.rect[1],dlgid.rect[4]-dlgid.rect[2])
	end
	local touchbeginpos=nil;
	local touchendpos=nil;
	local function onTouchBegan(touch, event)  
		touchbeginpos = touch:getLocation(); 
		return true;
	end
	local function onTouchEnded(touch, event)
		touchendpos = touch:getLocation();
		print("触摸坐标区域=",math.floor(touchendpos.x),math.floor(touchendpos.y))
		if not touchrect then  
			return;
		end
		if math.abs(touchendpos.x-touchbeginpos.x) < 20 and math.abs(touchendpos.y-touchbeginpos.y) < 20 then
			if not cc.rectContainsPoint(touchrect,touchendpos) then  
				view.closeDlg()
			end 
		end
	end
	local listener = cc.EventListenerTouchOneByOne:create();
	listener:setSwallowTouches(true);
	listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN);
	listener:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED);
	local eventDispatcher = layer:getEventDispatcher();
	eventDispatcher:addEventListenerWithSceneGraphPriority(listener,layer);
	if not dlgid.nomask then
		local layercolor = cc.LayerColor:create(cc.c4b(0,0,0,150)); 
	    layer:addChild(layercolor);
		layer:setContentSize(REAL_SCREEN_SIZE)
		local pos=cc.p(-WINDOW_CENTER.x,-WINDOW_CENTER.y)
		layercolor:setPosition(pos)
	end 
	return layer; 
end 


function ModalDlgManager:showDlg(dlgid,closeFunc,istop) 
    userop.userop("弹出模态框" .. dlgid.name)
    print("当前模态框",dlgid.name)
    local view = dlgid.view:create() 
	local scene= SceneManager:ins():getScene()
	view.closeDlg=nil
    local dlg = self:getDlg(dlgid,closeFunc,view) 
	dlg:addChild(view)	
	if not dlgid.noshake then 
		view:setScale(0.7)
		local t=0.1
		local s1=cc.ScaleTo:create(t,1.1)
		local s2=cc.ScaleTo:create(t,1)
        local function showEnd()
		end
		local call=cc.CallFunc:create(showEnd)
		local sequence=cc.Sequence:create(s1,s2,call)
		view:runAction(sequence)
	end
	if istop then
        --SceneManager:ins():addToExitLayer(dlg)
        SceneManager:ins():addToModelLayer(dlg)
		--dlg:setPosition(WINDOW_CENTER)
        --moveToFirst(dlg)--不符需求
		--cc.Director:getInstance():getRunningScene():addChild(dlg,10000)  
	else 
		SceneManager:ins():addToPopupLayer(dlg)
	end
	view.closeDlg=function()
		local function callback() 
			if closeFunc then 
				closeFunc()
			end
			dlg:removeFromParent()
			local scene=cc.Director:getInstance():getRunningScene()
			local control=scene.dicScene.control   
			if dlgid.name ~= 'Tips' and dlgid.name ~= 'Equip' then 
				if control.startGuide then  
					control:startGuide()
				end
			end
		end
		if dlgid.noshake then 
			callback()
		else
			local scale=cc.ScaleTo:create(0.2,0)
			
			local call=cc.CallFunc:create(callback)
			view:runAction(cc.Sequence:create(scale,call))
		end
	end 
    return view,dlg
end 
 
function ModalDlgManager:createDlg()
  local layer = cc.Layer:create() 
  local function onTouchBegan(touch, event)
    return true;
  end
  local listener = cc.EventListenerTouchOneByOne:create();
  listener:setSwallowTouches(true);
  listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN);
  local eventDispatcher = layer:getEventDispatcher(); 
  eventDispatcher:addEventListenerWithSceneGraphPriority(listener,layer); 
  return layer;
end 

 